using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Input;

namespace HB.MVVM
{

   
    /// <summary>
    /// 示例: public string str { get => GetString(); set => Set(value); }
    /// </summary>
    public class ViewModelBase : ViewModelBaseFody, INotifyPropertyChanged, IDisposable
    {
        #region get集合

        public byte GetByte(byte defaultValue = 0, [CallerMemberName] string key = null)
        {
            return Get<byte>(defaultValue, key);
        }
        public sbyte GetSbyte(sbyte defaultValue = 0, [CallerMemberName] string key = null)
        {
            return Get<sbyte>(defaultValue, key);
        }
        public short GetShort(short defaultValue = 0, [CallerMemberName] string key = null)
        {
            return Get(defaultValue, key);
        }
        public ushort GetUshort(ushort defaultValue = 0, [CallerMemberName] string key = null)
        {
            return Get(defaultValue, key);
        }
        public int GetInt(int defaultValue = 0, [CallerMemberName] string key = null)
        {
            return Get(defaultValue, key);
        }
        public uint GetUint(uint defaultValue = 0, [CallerMemberName] string key = null)
        {
            return Get(defaultValue, key);
        }
        public long GetLong(long defaultValue = 0, [CallerMemberName] string key = null)
        {
            return Get(defaultValue, key);
        }
        public ulong GetUlong(ulong defaultValue = 0, [CallerMemberName] string key = null)
        {
            return Get(defaultValue, key);
        }
        public float GetFloat(float defaultValue = 0f, [CallerMemberName] string key = null)
        {
            return Get(defaultValue, key);
        }
        public double GetDouble(double defaultValue = 0, [CallerMemberName] string key = null)
        {
            return Get(defaultValue, key);
        }
        public string GetString(string defaultValue = "", [CallerMemberName] string key = null)
        {
            return Get(defaultValue, key);
        }
        public bool GetBool(bool defaultValue = false, [CallerMemberName] string key = null)
        {
            return Get(defaultValue, key);
        }
        public char GetChar(char defaultValue = ' ', [CallerMemberName] string key = null)
        {
            return Get(defaultValue, key);
        }
        public decimal GetDecimal(decimal defaultValue = 0, [CallerMemberName] string key = null)
        {
            return Get(defaultValue, key);
        }

        #endregion

        #region 自管理字段set

        /// <summary>
        /// 设置值,通过值比较
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="field"></param>
        /// <param name="value"></param>
        /// <param name="key"></param>
        public void Set<T>(ref T field, T value, [CallerMemberName] string key = null)
        {
            if (!EqualityComparer<T>.Default.Equals(field, value))
            {
                field = value;
                OnPropertyChanged(key);
            }
        }
        #endregion


        #region 通用部分get set
        /// <summary>
        /// 托管存储
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="defaultValue"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public T Get<T>(T defaultValue = default(T), [CallerMemberName] string key = null)
        {
            if (dicObjList == null) { dicObjList = new Dictionary<string, IObjectList>(); }
            var typename = typeof(T).FullName;
            if (!dicObjList.ContainsKey(typename))
            {
                dicObjList[typename] = new ObjectList<T>();
            }
            Dictionary<string, T> dic = dicObjList[typename].GetList<T>();
            if (dic.ContainsKey(key))
            {
                return dic[key];
            }
            return defaultValue;
        }
        public void Set<T>(T value, [CallerMemberName] string key = null)
        {
            if (dicObjList == null) { dicObjList = new Dictionary<string, IObjectList>(); }
            var typename = typeof(T).FullName;
            if (!dicObjList.ContainsKey(typename))
            {
                dicObjList[typename] = new ObjectList<T>();
            }
            Dictionary<string, T> dic = dicObjList[typename].GetList<T>();
            bool ischanged = true;
            if (dic.ContainsKey(key))
            {
                if (EqualityComparer<T>.Default.Equals(dic[key], value))
                {
                    ischanged = false;
                }
            }
            if (ischanged)
            {
                dic[key] = value;
                OnPropertyChanged(key);
            }
        }
        #endregion

        Dictionary<string, IObjectList> dicObjList = null;

        /// <summary>
        /// 手动清理托管的变量存储
        /// </summary>
        public new void Dispose()
        {
            dicObjList?.Clear();
           
            base.Dispose();
        }
    }
}
